package com.yx.system.controller;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.yx.common.annotation.LoginUser2;
import com.yx.common.utils.PageUtils;
import com.yx.common.utils.Query;
import com.yx.common.utils.R;
import com.yx.common.validator.Assert;
import com.yx.modules.api.entity.TbAuthorApply;
import com.yx.modules.api.entity.TbUser;
import com.yx.pc.api.service.PcTbAuthorApplyService;
import com.yx.pc.api.service.PcTbUserService;
import com.yx.pc.api.service.PcTdBookService;
import com.yx.system.entity.SysUser;
import com.yx.system.entity.TdBook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;



/**
 * 普通作者申请
 * 
 * @author luo
 * @email www.baidu.com
 * @date 2018-07-02 10:26:26
 */
@RestController
@RequestMapping("sys/authorapply")
public class SysAuthorApplyController2 {
	@Autowired
	private PcTbAuthorApplyService authorApplyService;
	@Autowired
	private PcTbUserService tbUserService;
	@Autowired
	private PcTdBookService tdBookService;
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("sys:authorapply:list")
	public R list(@RequestParam Map<String, Object> params,@LoginUser2 SysUser sysUser){
		//查询用户
		TbUser tbUser = tbUserService.queryByMobile(sysUser.getUsername());
		//查询列表数据
        Query query = new Query(params);
        if (tbUser!=null){
        	query.put("userid",tbUser.getUserId());
		}

		List<TbAuthorApply> authorApplyList = authorApplyService.queryList(query);
		int total = authorApplyService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(authorApplyList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("sys:authorapply:info")
	public R info(@PathVariable("id") Integer  id){
		TbAuthorApply authorApply = authorApplyService.queryObject(id);
		
		return R.ok().put("authorApply", authorApply);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("sys:authorapply:save")
	public R save(@RequestBody TbAuthorApply authorApply){
		authorApplyService.save(authorApply);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("authorapply:update")
	public R update(@RequestBody TbAuthorApply authorApply){
		authorApplyService.update(authorApply);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("authorapply:delete")
	public R delete(@RequestBody Integer[] ids){
		authorApplyService.deleteBatch(ids);
		
		return R.ok();
	}

	/*
	* 申请通过
	*/
	@RequestMapping("/pass")
	@RequiresPermissions("sys:authorapply:pass")
	public R pass(@RequestBody TbAuthorApply authorApply, @LoginUser2 SysUser sysUser1) throws UnsupportedEncodingException {
		authorApply.setRefuseReason("");
		if (authorApply.getStatus()==1 || authorApply.getStatus()==2){
			return R.error("请选择正确的记录进行审核");
		}
		return authorApplyService.passApply(authorApply,sysUser1);

	}

	/*
	* 申请拒绝
	*/
	@RequestMapping("/refuse")
	@RequiresPermissions("sys:authorapply:refuse")
	public R refuse(@RequestBody TbAuthorApply authorApply, @LoginUser2 SysUser sysUser1) throws UnsupportedEncodingException {
		if (authorApply.getStatus()==1 || authorApply.getStatus()==2){
			return R.error("请选择正确的记录进行审核");
		}
		if (authorApply.getRefuseReason()==null||authorApply.getRefuseReason().equals("")){
			return R.error("拒绝原因不能为空");
		}
		return authorApplyService.refuseApply(authorApply,sysUser1);
	}

	/*
	* 查看作者申请
	*/
	@RequestMapping("/signedInfo/{id}")
	public R signedInfo(@PathVariable("id")Integer id){
		TbAuthorApply authorApply = authorApplyService.queryObject(id);
		if (authorApply==null){
			return R.error("未查询到此条记录");
		}
		if (authorApply.getType()==1){
			//找到用户
			TbUser tbUser = tbUserService.foundById(Long.valueOf(authorApply.getUserid()));
			if (tbUser==null){
				return R.error("用户不存在");
			}
			//
			List<TdBook> tdBooks = tdBookService.findByAuthorCode(tbUser.getUuid());
			if (tdBooks.size()==0){
				return R.error("该用户尚未写有书籍");
			}
			return R.ok().put("book",tdBooks);
		}else {
			return R.ok().put("data",authorApply);
		}
	}

	/*
	* 查看拒绝原因
	*/
	@RequestMapping("/refuseReasonInfo/{id}")
	public R refuseReasonInfo(@PathVariable("id")Integer id){
		TbAuthorApply authorApply = authorApplyService.queryObject(id);
		if (authorApply.getStatus()!=2){
			return R.error("请选择拒绝记录查看");
		}
		return R.ok().put("refuseReason",authorApply.getRefuseReason());
	}

	/*
	* 申请成为签约作者
	*/
	@RequestMapping("apply")
	public R apply(@LoginUser2 SysUser sysUser){
		if (sysUser.getIdentity()==0){
			return R.error("作者身份才能操作");
		}
		if (sysUser.getIdentity()==2){
			return R.error("你已经是签约作者了");
		}
		//根据系统用户去查询用户
		TbUser tbUser = tbUserService.queryByMobile(sysUser.getUsername());
		//首先查询作者之前的申请
		List<TbAuthorApply> tbAuthorApplies = authorApplyService.findAllBySysUser(tbUser.getUserId());
		//循环判断之前的申请
		for (TbAuthorApply tbAuthorApply:tbAuthorApplies){
			if (tbAuthorApply.getStatus()==0){
				return R.error("之前的申请还在审核中，请勿重复申请");
			}
		}
		//初始化作者申请对象
		TbAuthorApply tbAuthorApply = new TbAuthorApply();
		//赋值
		tbAuthorApply.setUserid(tbUser.getUserId());
		if (tbUser.getEmail()!=null){
			tbAuthorApply.setEmail(tbUser.getEmail());
		}
		tbAuthorApply.setCreatedate(new Date());
		tbAuthorApply.setNickname(tbUser.getNickname());
		tbAuthorApply.setUsername(tbUser.getMobile());
		tbAuthorApply.setPassword(tbUser.getPassword());
		tbAuthorApply.setStatus(0);
		tbAuthorApply.setType(1);
		authorApplyService.save(tbAuthorApply);
		return R.ok();
	}
}
